
import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpService } from 'src/app/shared/service/http.service';
import { SharePageComponent } from '../share-page/share-page.component';
import { AlertController, ModalController } from '@ionic/angular';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';

@Component({
  selector: 'app-url',
  templateUrl: './url.component.html',
  styleUrls: ['./url.component.scss'],
})
export class UrlComponent implements OnInit {

  @Input() tagData: any;

  title = '';
  url = '';
  titleBar: any; // 导航栏数据
  // 分享页面数据
  shareData = {
    icon: "share.png",
    layout: "right",
    submit: {
      content: {},
      id: "string"
    }
  };

  constructor(
    private route: ActivatedRoute,
    private http: HttpService,
    public alertController: AlertController,
    public appGlobalService: AppGlobalService,
    private modalController: ModalController,
  ) { }

  ngOnInit() {
    // console.log('tagData', this.tagData);
    this.route.queryParams.subscribe(p => {
      // console.log('queryParams', p);
      this.title = p.title;
      this.url = p.url;

      //  ? 后台需要支持登录  localStorage.getItem("loginStatus") !== null &&
      if (p.shareTarget) {
        this.titleBar = {
          share: {
            layout: "right",
            icon: "test.png",
            link: {
              type: "page",
              params: {
                city: p.shareCity,
                phone: p.sharePhone,
                name: p.shareName,
                opportunity_id: p.opportunity_id ? p.opportunity_id : '',
                plugin_id: p.plugin_id ? p.plugin_id : '',
                share: p.share
              },
              target: p.shareTarget
            }
          }
        };
      }
      if (p.target === "0") {
        const iframe = document.getElementById("iframe-url");
        iframe.setAttribute("src", this.url);
      } else {
        // 2023.12.26 防止转换报错
        if (this.tagData) {

          console.log('tagData === ', this.tagData);
          if (this.tagData.urlLinkTitle !== undefined) {
            this.title = this.tagData.urlLinkTitle;
            const iframe = document.getElementById("iframe-url");
            iframe.setAttribute("src", this.tagData.target);
          }
        } else {

          const pageParams = {
            pageId: p.target,
            params: JSON.parse(p.params),
            sessionId: localStorage.getItem("sessionId")
          };
          this.http.post('application/page', pageParams, res => {
            console.log(JSON.parse(res.script));
            this.titleBar = JSON.parse(res.script).titleBar;
            this.title = this.titleBar.title.labelName;
            // this.shareData = JSON.parse(res.script).views[0].tags[0].data;
            // console.log(this.shareData);
          });
          const iframe = document.getElementById("iframe-url");
          iframe.setAttribute("src", this.url);
        }


      }
    });
  }

  back() {
    this.appGlobalService.goBack();
  }

  async close() {
    const modal = await this.modalController.getTop();
    modal.dismiss();
    return;
  }


  // 分享弹框
  async presentModal() {
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: 'sharePage',
      backdropDismiss: true,
      component: SharePageComponent,
      componentProps: { tagData: this.shareData }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData);
  }
  /** 分享 */
  share(link) {
    console.log('share --> page', link);
    // 请求参数
    const params = {
      sessionId: localStorage.getItem("sessionId"),
      pageId: link.target,
      params: link.params
    };
    this.http.post('application/page', params, res => {
      console.log(JSON.parse(res.script));
      this.shareData = JSON.parse(res.script).views[0].tags[0].data;
      console.log(this.shareData);
      this.presentModal();
    });
  }
}
